package cn.iocoder.springboot.labs.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID;

@RestController
public class SpringSessionJdbcController {

    private static final String CREATE_SESSION_ATTRIBUTE_QUERY =
            "INSERT INTO SPRING_SESSION_ATTRIBUTES(SESSION_ID, ATTRIBUTE_NAME) " +
                    "SELECT SESSION_ID, ?" +
                    "FROM SPRING_SESSION " +
                    "WHERE SESSION_ID = ?";
    @Autowired
    private DataSourceProperties dataSourceProperties;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @GetMapping("/save")
    @ResponseBody
    public String index(HttpServletRequest request) {
        request.getSession().setAttribute("uuid", UUID.randomUUID().toString());
        return "保存session属性成功";
    }

    @GetMapping("/remove")
    @ResponseBody
    public String remove(HttpServletRequest request) {
        request.getSession().removeAttribute("uuid");
        return "删除session属性成功";
    }

    @GetMapping("/jdbcTemplateTest")
    public String jdbcTemplateTest() {
        jdbcTemplate.update(CREATE_SESSION_ATTRIBUTE_QUERY, ps -> {
            ps.setString(1, "test");
            ps.setString(2, UUID.randomUUID().toString());
        });

        jdbcTemplate.update("delete from SPRING_SESSION_ATTRIBUTES where ATTRIBUTE_NAME='test'");
        return "jdbctemplate 插入session属性成功";
    }

    @GetMapping("/jdbcTest")
    public String jdbcTest() throws SQLException {
        System.out.println(dataSourceProperties.getUrl());
        System.out.println(dataSourceProperties.getUsername());
        System.out.println(dataSourceProperties.getPassword());
        Connection connection = DriverManager.getConnection(dataSourceProperties.getUrl(), dataSourceProperties.getUsername(), dataSourceProperties.getPassword());
        PreparedStatement ps = connection.prepareStatement(CREATE_SESSION_ATTRIBUTE_QUERY);
        ps.setString(1, "test");
        ps.setString(2, UUID.randomUUID().toString());
        ps.executeUpdate();

        ps = connection.prepareStatement("delete from SPRING_SESSION_ATTRIBUTES where ATTRIBUTE_NAME='test'");
        ps.executeUpdate();
        return "jdbc 插入session属性成功";
    }


    @GetMapping("/jdbcDelTest")
    public String jdbcDelTest() throws SQLException {
        System.out.println(dataSourceProperties.getUrl());
        System.out.println(dataSourceProperties.getUsername());
        System.out.println(dataSourceProperties.getPassword());
        Connection connection = DriverManager.getConnection(dataSourceProperties.getUrl(), dataSourceProperties.getUsername(), dataSourceProperties.getPassword());
        PreparedStatement ps = connection.prepareStatement("DELETE FROM SPRING_SESSION WHERE SESSION_ID = ?");
        ps.setString(1, UUID.randomUUID().toString());
        ps.executeUpdate();
        return "success";
    }


}
